package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.BankCardAdapter;
import com.niji.littly_donkey_android.adapter.FollowAdapter;
import com.niji.littly_donkey_android.bean.BankCard;
import com.niji.littly_donkey_android.bean.Follow;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BillBankCardActivity extends BaseActivity {

    private final static String TAG = "BillBankCardActivity";

    private Button bill_bank_return;

    private TextView add_bank_card;

    private Button add_bank_card_btn;

    private ListView bank_card_list;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill_activity);
        billBankReturn();
        addBankCard();
        addBankCardBtn();
        showBankList();
        selectBankCard();
    }

    /**
     * 选择银行卡
     */
    private void selectBankCard() {
        bank_card_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView bank_card_id = view.findViewById(R.id.bank_card_id);
                TextView bank_card_name = view.findViewById(R.id.bank_card_name);
                TextView bank_card_number = view.findViewById(R.id.bank_card_number);
                Integer bankCardId = Integer.parseInt(bank_card_id.getText().toString().trim());
                String bankCardName = bank_card_name.getText().toString().trim();
                String bankCardNumber = bank_card_number.getText().toString().trim();
                BankCard bankCard = new BankCard();
                bankCard.setId(bankCardId);
                bankCard.setName(bankCardName);
                bankCard.setNumber(bankCardNumber);

                Log.i("bankCard",bankCard.toString());
                Intent intent1 = getIntent();
                String className = intent1.getStringExtra("className");

                if (className != null){
                    if(className.equals("BillBillActivity")){
                        Intent intent = new Intent(BillBankCardActivity.this,BillBillActivity.class);
                        intent.putExtra("bankCard",bankCard);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }

    /**
     * 银行卡展示
     */
    private void showBankList() {
        bank_card_list = findViewById(R.id.bank_card_list);
        String url = UrlUtil.URL + "/commission/bank/card";
        ArrayList<BankCard> bankCards = new ArrayList<>();
        RequestQueue requestQueue= Volley.newRequestQueue(BillBankCardActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            BankCard bankCard = new BankCard();
                            Integer id = (Integer) mapList.get(i).get("id");
                            String name = (String) mapList.get(i).get("name");
                            String number = (String) mapList.get(i).get("number");
                            Boolean is_default = (Boolean) mapList.get(i).get("is_default");
                            bankCard.setId(id);
                            bankCard.setName(name);
                            bankCard.setNumber(number);
                            bankCard.setDefault(is_default);
                            bankCards.add(bankCard);

                        }

                    } else if(code == -1){
                        Toast.makeText(BillBankCardActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(BillBankCardActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(BillBankCardActivity.this, "银行卡列表获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                bank_card_list.setAdapter(new BankCardAdapter(BillBankCardActivity.this,bankCards));
                RelativeLayout bank_card_loading = findViewById(R.id.bank_card_loading);
                RelativeLayout bank_card_main = findViewById(R.id.bank_card_main);
                bank_card_main.setVisibility(View.VISIBLE);
                bank_card_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(BillBankCardActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.bill_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    /***
     * 添加银行卡按钮
     */
    private void addBankCardBtn() {
        add_bank_card_btn = findViewById(R.id.add_bank_card_btn);
        add_bank_card_btn = findViewById(R.id.add_bank_card_btn);
        add_bank_card_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BillBankCardActivity.this, AddlBankCardActivity.class));
            }
        });
    }

    private void addBankCard() {
        add_bank_card = findViewById(R.id.add_bank_card);
        add_bank_card = findViewById(R.id.add_bank_card);
        add_bank_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BillBankCardActivity.this, AddlBankCardActivity.class));
            }
        });
    }

    /**
     * 返回
     */
    private void billBankReturn() {
        bill_bank_return = findViewById(R.id.bill_bank_return);
        bill_bank_return = findViewById(R.id.bill_bank_return);
        bill_bank_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }

}
